package com.essc.controller;

import com.essc.dto.OrderDto;
import com.essc.pojo.Order;
import com.essc.service.OrderService;

import com.essc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /*去购买--生成订单（处理中 status=1）*/
    /*先生成 status=1的处理中订单 由买家选择交易方式click事件 根据不同的交易方式显示不同信息*/
    /*需要传入
    userId goodId*/
    @PostMapping("/toBuy")
    public JsonResult makeOrder(@RequestBody Order order){
        return orderService.makeOrder(order);
    }

    /*去支付---支付订单->订单完成/订单失效*/
    /*需要传入buyerId gid oid send time*/
    @PostMapping("/toPay")
    public JsonResult payOrder(@RequestBody OrderDto orderDto){
        System.out.println("接收到了请求");
        return orderService.payOrder(orderDto);
    }

    /*待支付显示订单 新增洪志成*/
    /*需要传入
    userId goodId*/
    @PostMapping("/showOrder")
    public JsonResult showOrder(@RequestBody Order order){
        return orderService.showOrder(order);
    }

    /*取消订单、支付失败（ status=0）*/
    /*需要传入oid*/
    @PostMapping("/cancelOrder/{oid}")
    public JsonResult cancelOrder(@PathVariable Integer oid){
        return orderService.cancelOrder(oid);
    }

    /*确认发货 卖家确认发货-根据oid更新order.status为3*/
    /*需要传入oid*/
    @PostMapping("/confirmSendOrder/{oid}")
    public JsonResult confirmSendOrder(@PathVariable Integer oid){
        return orderService.confirmSendOrder(oid);
    };

    /*确认签收 买家确认收货-根据oid更新order.status为4*/
    /*需要传入oid*/
    @PostMapping("/confirmReceipt/{oid}")
    public JsonResult confirmReceipt (@PathVariable Integer oid){
        return orderService.confirmReceipt(oid);
    };

    /*发起退货请求 买家发起退货请求-根据oid更新order.status为-1*/
    /*需要传入oid*/
    @PostMapping("/requestGoodRejected/{oid}")
    public JsonResult requestGoodRejected(@PathVariable Integer oid){
        return orderService.requestGoodRejected(oid);
    };

    /*同意退货 卖家同意退货请求-根据oid更新order.status为0*/
    /*需要传入oid*/
    @PostMapping("/agreeGoodRejected/{oid}")
    public JsonResult agreeGoodRejected(@PathVariable Integer oid){
        return orderService.agreeGoodRejected(oid);
    };

    /*----------------------------------买家用户查订单------------------------------------*/

    /*用户查看待支付的订单 (status=1)*/
    /*需要传入uid pageNum pageSize*/
    @GetMapping("/findOrderInP/{userid}/{pageNum}/{pageSize}")
    public JsonResult findOrderInProgress(@PathVariable("userid") Integer uid,
                                          @PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize){
        return orderService.findOrderInProgress(uid,pageNum,pageSize);
    }

    /*用户查看失效订单 (status=0)*/
    /*需要传入userid pageNum pageSize*/
    @GetMapping("/findInvalidOrder/{userid}/{pageNum}/{pageSize}")
    public JsonResult findInvalidOrder(@PathVariable("userid") Integer uid,
                                       @PathVariable("pageNum") Integer pageNum,
                                       @PathVariable("pageSize") Integer pageSize){
        return orderService.findInvalidOrder(uid,pageNum,pageSize);
    }

    /*用户查看已支付订单 (status=2)*/
    /*需要传入userid pageNum pageSize*/
    @GetMapping("/findCompletedOrder/{userid}/{pageNum}/{pageSize}")
    public JsonResult findCompletedOrder(@PathVariable("userid") Integer uid,
                                         @PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize){
        return orderService.findCompletedOrder(uid,pageNum,pageSize);
    }

    /*!!买家用户查看已发货待签收订单 order.status为3*/
    /*需要传入userid pageNum pageSize*/
    @GetMapping("/findOrderToBeSigned/{userid}/{pageNum}/{pageSize}")
    public JsonResult findOrderToBeSigned(@PathVariable("userid") Integer uid,
                                          @PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize){
        return orderService.findOrderToBeSigned(uid,pageNum,pageSize);
    }

    /*!!买家用户查看已签收订单 order.status为4*/
    /*需要传入userid pageNum pageSize*/
    @GetMapping("/findOrderSigned/{userid}/{pageNum}/{pageSize}")
    public JsonResult findOrderSigned(@PathVariable("userid")Integer uid,
                                      @PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize){
        return orderService.findOrderSigned(uid,pageNum,pageSize);
    }

    /*!!买家用户查退款中订单 order.status为-1*/
    /*需要传入userid pageNum pageSize*/
    @GetMapping("/findOrderRefunding/{userid}/{pageNum}/{pageSize}")
    public JsonResult findOrderRefunding(@PathVariable("userid") Integer uid,
                                         @PathVariable("pageNum") Integer pageNum,
                                         @PathVariable("pageSize") Integer pageSize){
        return orderService.findOrderRefunding(uid,pageNum,pageSize);
    }

    /*----------------------------------卖家用户查订单------------------------------------*/

    /*卖家用户在商品管理查看已完成订单 (status=2)*/
    @GetMapping("/findSellOrder/{gid}")
    public JsonResult findSellOrder(@PathVariable("gid") Integer gid){
        return orderService.findSellOrder(gid);
    }

    /*卖家用户在订单管理查看所有已支付订单 order.status为2*/
    /*分页 pageNum当前页 pageSize每页数量*/
    /*前端传入uid 【即当前用户id也是充当卖家用户的id】
              pageNum pageSize       */
    @GetMapping("/findSellCompletedOrder/{uid}/{pageNum}/{pageSize}")
    public JsonResult findSellCompletedOrder(@PathVariable("uid") Integer uid,
                                             @PathVariable("pageNum") Integer pageNum,
                                             @PathVariable("pageSize") Integer pageSize){
        return orderService.findSellCompletedOrder(uid,pageNum,pageSize);
    }

    /*卖家用户查看订单管理 待签收订单status=3*/
    @GetMapping("/findSellOrderToBeSigned/{uid}/{pageNum}/{pageSize}")
    public JsonResult findSellOrderToBeSigned(@PathVariable("uid") Integer uid,
                                              @PathVariable("pageNum") Integer pageNum,
                                              @PathVariable("pageSize") Integer pageSize){
        return orderService.findSellOrderToBeSigned(uid,pageNum,pageSize);
    }

    /*卖家用户查看订单管理 已签收订单status=4*/
    @GetMapping("/findSellOrderSigned/{uid}/{pageNum}/{pageSize}")
    public JsonResult findSellOrderSigned(@PathVariable("uid") Integer uid,
                                          @PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize){
        return orderService.findSellOrderSigned(uid,pageNum,pageSize);
    }

    /*卖家用户查看订单管理 待退款订单status=-1*/
    @GetMapping("/findSellRefundedOrder/{uid}/{pageNum}/{pageSize}")
    public JsonResult findSellRefundedOrder(@PathVariable("uid") Integer uid,
                                            @PathVariable("pageNum") Integer pageNum,
                                            @PathVariable("pageSize") Integer pageSize){
        return orderService.findSellRefundedOrder(uid,pageNum,pageSize);
    }

    /*卖家用户查看订单管理 已失效订单status=0*/
    @GetMapping("/findSellInvalidOrder/{uid}/{pageNum}/{pageSize}")
    public JsonResult findSellInvalidOrder(@PathVariable("uid") Integer uid,
                                           @PathVariable("pageNum") Integer pageNum,
                                           @PathVariable("pageSize") Integer pageSize){
        return orderService.findSellInvalidOrder(uid,pageNum,pageSize);
    }

}
